Imports System.Linq
Imports System.Data.SqlClient
Imports System.Collections.Generic
Imports SISSAT.BusinessLayer.DomainModels
Imports SISSAT.DataAcessLayer.StatementSources
Imports SISSAT.DataAcessLayer.PersistentStorage

Namespace Services
    Public Class ExaminationService
        Inherits ServiceBase
        Implements IExaminationService

        Public Sub Create(entities As DataTable, createdBy As Integer) Implements IExaminationService.Create
            If entities.Rows.Count() = 0 Then
                Return
            End If

            StatementSource = New CreateExaminationStatementSource(entities, createdBy)
            Dim nonQuery As Integer = DbHelper.ExecuteNonQuery(StatementSource)
        End Sub

        Public Sub Delete(examinationId As Integer) Implements IExaminationService.Delete
            StatementSource = New DeleteExaminationStatementSource(examinationId)
            Dim nonQuery As Integer = DbHelper.ExecuteNonQuery(StatementSource)
        End Sub

        Public Sub Edit(entities As DataTable, modifiedBy As Integer) Implements IExaminationService.Edit
            If entities.Rows.Count() = 0 Then
                Return
            End If

            StatementSource = New UpdateExaminationStatementSource(entities, modifiedBy)
            Dim nonQuery As Integer = DbHelper.ExecuteNonQuery(StatementSource)
        End Sub

        Public Function ReadAll() As IEnumerable(Of Examination) Implements IExaminationService.ReadAll
            StatementSource = New ReadAllExaminationStatementSource()
            Dim dataReader As SqlDataReader = DbHelper.ExecuteReader(StatementSource)

            Return MapDataToEntity(dataReader)
        End Function

        Public Function ReadAll(studentCode As String, scheduleId As Integer, groupId As Integer) As IEnumerable(Of Examination) Implements IExaminationService.ReadAll
            StatementSource = New ReadAllExaminationStatementSource(studentCode, scheduleId, groupId)
            Dim dataReader As SqlDataReader = DbHelper.ExecuteReader(StatementSource)

            Return MapDataToEntity(dataReader)
        End Function

        Public Function ReadById(examinationId As Integer) As Examination Implements IExaminationService.ReadById
            StatementSource = New ReadByExaminationIdStatementSource(examinationId)
            Dim dataReader As SqlDataReader = DbHelper.ExecuteReader(StatementSource)

            Return MapDataToEntity(dataReader).FirstOrDefault()
        End Function

        Public Function ReadAllFilteredByMajorExams(studentCode As String, scheduleId As Integer) As IEnumerable(Of Examination) Implements IExaminationService.ReadAllFilteredByMajorExams
            StatementSource = New ReadAllExaminationStatementSource(studentCode, scheduleId)
            Dim dataReader As SqlDataReader = DbHelper.ExecuteReader(StatementSource)

            Return MapDataToEntity(dataReader)
        End Function

        Public Function ReadByStudentCode(studentCode As String, scheduleId As Integer, groupId As Integer, examinationTypeId As Integer) As Examination Implements IExaminationService.ReadByStudentCode
            StatementSource = New ReadAllExaminationStatementSource(studentCode, scheduleId, groupId, examinationTypeId)
            Dim dataReader As SqlDataReader = DbHelper.ExecuteReader(StatementSource)

            Return MapDataToEntity(dataReader).FirstOrDefault()
        End Function

        Private Function MapDataToEntity(dataReader As SqlDataReader) As IEnumerable(Of Examination)
            Dim entities As New List(Of Examination)

            If (dataReader.HasRows) Then
                Dim typeService As IExaminationTypeService = New ExaminationTypeService()
                Dim scheduleService As IScheduleService = New ScheduleService()

                While (dataReader.Read())
                    Dim entity As New Examination
                    Dim examinationTypeId As Integer = Convert.ToInt32(dataReader("ExaminationTypeId"))
                    Dim scheduleId As Integer = Convert.ToInt32(dataReader("ScheduleId"))

                    entity.Grade = Convert.ToInt32(dataReader("Grade"))
                    entity.GroupId = Convert.ToInt32(dataReader("GroupId"))
                    entity.ExaminationId = Convert.ToInt32(dataReader("ExaminationId"))
                    entity.Schedule = scheduleService.ReadById(scheduleId)
                    entity.StudentCode = Convert.ToString(dataReader("StudentCode"))
                    entity.ExaminationType = typeService.ReadById(examinationTypeId)

                    entities.Add(entity)
                End While
            End If

            Return entities
        End Function

    End Class
End Namespace